package com.chapter09;

import com.bdqn.util.JDBCUtils;
import com.pojo.Grade;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

public class Test03_QueryRunner {
    public static void main(String[] args) throws Exception {
        // todo 1、创建数据源
        Connection connection = JDBCUtils.getConnection();

        // todo 2、创建QueryRunner对象
        QueryRunner queryRunner = new QueryRunner();

        // todo 3、查
//        String sql = "SELECT GradeId,GradeName FROM grade WHERE GradeId=?;";
//        ResultSetHandler<Grade> rsh = new BeanHandler<Grade>(Grade.class);
//        Grade grade = queryRunner.<Grade>query(connection, sql, rsh, 100);
//        System.out.println(grade);

        String sql = "SELECT GradeId,GradeName FROM grade;";
//        BeanListHandler<Grade> rsh = new BeanListHandler<>(Grade.class);


        CustomStringHandler customStringHandler = new CustomStringHandler();

        String query = queryRunner.query(connection, sql, customStringHandler);
        System.out.println(query);
//        for (Grade grade : grades) {
//            System.out.println(grade);
//        }
    }
}
